#!/usr/bin/perl

# http://vergil.chemistry.gatech.edu/resources/programming/perl-tutorial/process.html
# This script will try to bring up a slip interface given 
#  a vtty and optional IP address information
print "args = ".@ARGV."\n";
if (@ARGV < 3) {
   print "Usage: hdwr_svr_helper slipup ttypath network_interface netmask\n";
   print "   Ex: hdwr_svr_helper slipup /dev/pts/1 192.168.1.1 255.255.255.0\n";
   die("exiting...");
}

if (@ARGV[0] != "slipup") {
  die ("bad command type");
}

$tty_path = @ARGV[1];
$gateway_address =  @ARGV[2];
$ifconfig_netmask = @ARGV[3];

print "tty path = ".$tty_path."\n";
print "gateway_address = ".$gateway_address."\n";
print "netmask = ".$ifconfig_netmask."\n";

$slattachout = `/sbin/slattach -ed -p slip $tty_path`;
print "slattach output:\n\n".$slattachout."\n";

$slip_interface = "uninitialized";

$ind = index($slattachout, " interface sl");
print "slindex = $ind \n";
if ($ind > 0) {
  $slip_interface = substr($slattachout, $ind + 11, 3);
} else {
  print "slattach output error:\n\n".$slattachout."\n";
  die("slattach output error");
}

print "slipifc = '$slip_interface' \n";
print "slipifc len = ".length($slip_interface)."\n";

$ifcfgout = `/sbin/ifconfig $slip_interface $gateway_address mtu 296 netmask $ifconfig_netmask up`;

#$ifcfgout = `echo $slip_interface $gateway_address mtu 296 netmask $ifconfig_netmask up`;

print "ifconfig output:\n\n";
print $ifcfgout;
print "\n\ndone.\n\n"
